﻿using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
using MvvmFx.Common.Helpers;


namespace MvvmFx.Common.Converters
{
    /// <summary>
    /// Converter used to create a <see cref="SolidColorBrush"/> from a
    /// <see cref="Color"/>.
    /// </summary>
    public class ColorToSolidColorBrushConverter : IValueConverter
    {
        #region class variables

        private static readonly Brush _nullBrush;

        #endregion


        #region constructors

        static ColorToSolidColorBrushConverter()
        {
            // initialize class variables
            _nullBrush = new SolidColorBrush(Colors.Transparent);
        }

        #endregion


        #region public methods

        public object Convert(object value, Type targetType, object parameter,
            CultureInfo culture)
        {
            // return null brush if none is specified
            if (ConverterHelper.IsNullOrUnset(value) == true)
            {
                return _nullBrush;
            }

            // create corresponding brush
            Color color = (Color)value;
            return new SolidColorBrush(color);
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            SolidColorBrush brush = (SolidColorBrush)value;
            return brush.Color;
        }

        #endregion

    }  // class ColorToSolidColorBrushConverter

}  // namespace MvvmFx.Common.Converters